package com.test.mchelepesa.adapter_rice;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.test.mchelepesa.adapter_rice.base_rice.BaseListAdapter_rice;
import com.test.mchelepesa.bean_rice.config_info_rice.AppShowInfo_rice;
import com.test.mchelepesa.bean_rice.config_info_rice.CustomerService_rice;
import com.test.mchelepesa.R;
import com.test.mchelepesa.constant_rice.Constant_rice;
import com.test.mchelepesa.util_rice.StringUtil_rice;

import java.util.ArrayList;


public class CustomerServiceListAdapter_rice extends BaseListAdapter_rice {

    private final Context context;

    private static String hotlineTitle = "";
    private static String emailTitle = "";

    private final ArrayList<CustomerService_rice> customerServices = new ArrayList<>();

    public CustomerServiceListAdapter_rice(Context context){
        this.context = context;
        hotlineTitle = context.getString(R.string.customer_service_hotline);
        emailTitle = context.getString(R.string.customer_service_email);
        initCustomerService();
    }

    @Override
    public int getCount() {
        return customerServices.size();
    }

    @Override
    public CustomerService_rice getItem(int position) {
        return customerServices.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void clear(){
        customerServices.clear();
    }

    public void add(CustomerService_rice customerService){
        customerServices.add(customerService);
    }

    @SuppressLint({"SetTextI18n", "InflateParams"})
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        if (view==null) view = LayoutInflater.from(context).inflate(R.layout.item_customer_service_rice,parent,false);

        TextView titleTv = view.findViewById(R.id.item_customer_service_tv_title);
        TextView textTv = view.findViewById(R.id.item_customer_service_tv_text);
        ViewGroup btnLayout = view.findViewById(R.id.item_customer_service_layout_btn);
        ImageView btnIv  = view.findViewById(R.id.item_customer_service_iv_btn);
        TextView btnTv = view.findViewById(R.id.item_customer_service_tv_btn);

        CustomerService_rice customerService = customerServices.get(position);

        titleTv.setText(customerService.getServiceTitle());
        String text = customerService.getServiceText();
        textTv.setText(text);

        if (customerService.getServiceType() == CustomerService_rice.CustomerServiceType.Hotline){
            btnIv.setImageResource(R.drawable.icon_dial);
            btnTv.setText(context.getString(R.string.word_dial));
            btnLayout.setVisibility(VISIBLE);
            btnLayout.setOnClickListener(v -> {
                try {
                    String mobile =customerService.getServiceText().trim();
                    String[] mobileArr = mobile.split(" ");
                    if (mobileArr.length>0) mobile = mobileArr[mobileArr.length-1];
                    Intent dialIntent_juucash =  new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mobile));//跳转到拨号界面，同时传递电话号码
                    context.startActivity(dialIntent_juucash);
                }catch (Exception e){
                    StringUtil_rice.printException(e);
                }
            });
        }else if (customerService.getServiceType() == CustomerService_rice.CustomerServiceType.Email){
            btnLayout.setVisibility(GONE);
        }

        return view;
    }

    public void initCustomerService(){
        clear();
        //添加服务热线
        String[] serviceHotlines = AppShowInfo_rice.getInstance().getMobile();
        for (String serviceHotline:serviceHotlines){
            if (!serviceHotline.isEmpty()){
                add(new CustomerService_rice(
                        CustomerService_rice.CustomerServiceType.Hotline,
                        hotlineTitle,
                        serviceHotline
                ));
            }
        }

        //添加服务邮箱
        String[] serviceEmails = AppShowInfo_rice.getInstance().getEmail();
        for (String serviceEmail:serviceEmails){
            if (!serviceEmail.isEmpty()){
                add(new CustomerService_rice(
                        CustomerService_rice.CustomerServiceType.Email,
                        emailTitle,
                        serviceEmail
                ));
            }
        }
    }
}
